package javazoom.jlGui.skin;

/**
 * This class implements a Skin Loader. WinAmp 2.x skins compliant.
 *
 *-----------------------------------------------------------------------
 *  This program is free software; you can redistribute it and/or modify
 *  it under the terms of the GNU General Public License as published by
 *  the Free Software Foundation; either version 2 of the License, or
 *  (at your option) any later version.
 *
 *  This program is distributed in the hope that it will be useful,
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *  GNU General Public License for more details.
 *
 *  You should have received a copy of the GNU General Public License
 *  along with this program; if not, write to the Free Software
 *  Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
 *----------------------------------------------------------------------
 */

import java.awt.Image;
import java.io.*;
import java.util.*;
import java.util.zip.ZipEntry;
import java.util.zip.ZipFile;

import javazoom.Util.*;

/**
 * This class implements a Skin Loader.
 * WinAmp 2.x skins compliant.
 */
public class SkinLoader
{
    private Hashtable 	_images = null;
	private String		_filename = null;

    public SkinLoader(String filename)
    {
        _images = new Hashtable();
        _filename = filename;
    }

    public void loadImages() throws Exception
    {
        ZipFile zf = new ZipFile(_filename);
        Enumeration e = zf.entries();
        while (e.hasMoreElements())
        {
            ZipEntry entry = (ZipEntry) e.nextElement();
            if(entry.getName().toLowerCase().endsWith("bmp"))
            {
                BMPLoader bmp = new BMPLoader();
                ByteArrayOutputStream baos = new ByteArrayOutputStream();
                InputStream is = zf.getInputStream(entry);
                byte buffer[] = new byte[256];
                do
                {
                    int bytesRead = is.read(buffer);
                    if(bytesRead == -1)
                        break;
                    baos.write(buffer, 0, bytesRead);
                } while(true);
                is.close();
                baos.close();
                StringTokenizer strtoken = new StringTokenizer(entry.getName().toLowerCase(), "/");
                String name;
                for(name = entry.getName().toLowerCase(); strtoken.hasMoreTokens(); name = (String)strtoken.nextElement())
                {
                	_images.put(name, bmp.getBMPImage(new ByteArrayInputStream(baos.toByteArray())));
				}
            }
        }
        zf.close();
    }
	/**
	 * Return Image from name.
	 */
    public Image getImage(String name)
    {
        return (Image)_images.get(name);
    }
}
